<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
    <script src="./js/jquery.js"></script>
    <style>
      * {
        margin: 0;
        padding: 0;
      }
      .test_checks {
        width: 400px;
        height: 200px;

        padding: 10px;
        margin: 10px auto;
        text-align: center;
        line-height: 30px;
        font-size: 14px;
        color: #000;
        background-color: #fff;
        box-shadow: 0 0 10px rgba(0, 0, 0, 0.3);
        position: relative;
        overflow: hidden;
      }
    </style>
  </head>
  <body>
    <div class="test_checks">
      <h2>JavaScript原生全选、反选功能</h2>
      <input type="checkbox" id="selectAll" /> 全选
      <br />
      <br />
      <input type="checkbox" class="checkbox" /> Item 1
      <br />
      <input type="checkbox" class="checkbox" /> Item 2
      <br />
      <input type="checkbox" class="checkbox" /> Item 3
      <br />
    </div>

    <script>
        $(function(){
         $("#selectAll").change(function(){
            if($(this).prop("checked")){

                $(".checkbox").prop("checked",true)
            }else{
                $(".checkbox").prop("checked",false)
            }
         })

         $(".checkbox").change(function(){
            if($(".checkbox:checked").length==$(".checkbox").length){
                $("#selectAll").prop("checked",true)
            }else{
                $("#selectAll").prop("checked",false)
            }
         })


        })
    </script>
  </body>
</html>
